--[[
FilePath: \Lua\View\UIActivityCard\UIActivityCard.lua
祈愿星牌活动
--]]

require 'View/UIBase/UIBase'

UIActivityCard = UIBase.New(PanelNames.UIActivityCard)
local _M = UIActivityCard

_M.hasInit = false
_M.playEffect = false;
_M.isClick = false;
_M.selectTabIndex = 1;
_M.selectCardIndex = 1;
_M.cardItems = nil;
_M.selectItem = nil;
_M.selectItemIndex = 0;
_M.cardObjItems = nil;
_M.cardRewardObjItems = nil;
_M.hasGetBigReward = false;
_M.getRewardNum = 0;
_M.cardEffectTimer = nil;
_M.shuiPaoObj = nil;
_M.shuiPaoObj1 = nil;
_M.effectColor = nil;
_M.beEffectColor1 = nil;
local SPECIAL_RECHARGE_TYPE = 4;
local effectColor1 = Color.New(76/255, 151/255, 255/255, 1);
local effectColor2 = Color.New(255/255, 223/255, 102/255, 1);
local effectColor3 = Color.New(255/255, 86/255, 81/255, 1);
local effectColor4 = Color.New(255/255, 154/255, 136/255, 1);

function _M.Init(func, id)
    _M.activeId = id
    UIBase.Init(_M, func)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("Backbtn"), self.OnBack);
        self.btnHelp = self:GetChild("help");
        self.CashLb = self:GetChild("CoinLb");
        self.CoinLb = self:GetChild("CoinLb1");
        self.title = self:GetChild("title"):GetChild("title");
        self.list = self:GetChild("List");
        self.needItem = self:GetChild("needItem");
        self.costIcon = self.needItem:GetChild("icon");
        self.costNum = self.needItem:GetChild("title");
        self.taskList = self:GetChild("TaskList");
        self.cardList = self:GetChild("CardList");
        self.shopBtn = self:GetChild("ShopBtn");
        self.btnAddLv = self:GetChild("btn_addLv");
        self.BtnSign1 = self:GetChild("BtnSign1");
        self.BtnSign2 = self:GetChild("BtnSign2");
        self.btnReset = self:GetChild("btn_reset");
        self.typeCtrl = self:GetController("Type");
        self.redCtrl = self.BtnSign2:GetChild("UIRedDots"):GetController("c1");
        self.typeCtrl.selectedIndex = 0;
        self.BtnSign1.selected = true;
        self.BtnSign2.selected = false;


        self.cardShowCtrl = self:GetController("CardShow");
        self.colorTypeCtrl = self:GetController("ColorType");
        self.colorTypeCtrl1 = self:GetChild("shine_01"):GetController("ColorTayp");
        self.colorTypeCtrl2 = self:GetChild("shine_02"):GetController("ColorTayp");
        self.loopCtrl = self:GetController("loop");
        self.glowCtrl = self:GetController("glow");
        --self.cardShowCtrl.selectedIndex = 0;
        self.effectNode = self:GetChild("eff_chouka_dot");
        self.effectNode1 = self:GetChild("eff_chouka_dot_01");
        local showCard = self:GetChild("ButtonCard");
        self.getCardIcon = showCard:GetChild("BG");
        showCard:GetController("Spine").selectedIndex = 1;
        --self.spineFore = self:GetChild("Spine_fore");
        self.bgClose = self:GetChild("bgClose");
        self.cardTrans = self:GetTransition("ButtonCard");
        self.effectXunhuan = self:GetTransition("Effect_xunhuan");
        self.effectSanguang = self:GetTransition("Effect_sanguang");

        self.list.itemRenderer = self.refreshCardItem;
        self.taskList.itemRenderer = self.refreshTaskItem;
        self.cardList.itemRenderer = self.refreshCardRewardItem;

        _M.hasInit = true

        self:addEvent()
    end
end

function _M:addEvent()
    self:RegisterEvent(Config.EventType.Fresh_Active,self.Refresh);
    self.btnHelp.onClick:Add(self.HelpTip, self)
    self.shopBtn.onClick:Add(self.ClickShopBtn, self)
    self.btnAddLv.onClick:Add(self.ClickBtnAddLv, self)
    self.BtnSign1.onClick:Add(self.ClickBtnSign1, self)
    self.BtnSign2.onClick:Add(self.ClickBtnSign2, self)
    self.btnReset.onClick:Add(self.ClickBtnReset, self)
end

function _M:removeEvent()
    self.btnHelp.onClick:Remove(self.HelpTip)
    self.shopBtn.onClick:Remove(self.ClickShopBtn)
    self.btnAddLv.onClick:Remove(self.ClickBtnAddLv)
    self.BtnSign1.onClick:Remove(self.ClickBtnSign1)
    self.BtnSign2.onClick:Remove(self.ClickBtnSign2)
    self.btnReset.onClick:Remove(self.ClickBtnReset)
end

function _M:HelpTip()
    if self.tableData ~= nil and self.tableData.f_ExplainTitle ~= nil and self.tableData.f_ExplainText ~= nil then
        logicMgr.UIShowManager.ShowCommonTips(tostring(self.tableData.f_ExplainTitle), tostring(self.tableData.f_ExplainText));
    end
end

function _M:ClickShopBtn()
    --if self.selectCardIndex == 1 then
    --    self.typeCtrl.selectedIndex = 2;
    --    self.selectCardIndex = 2;
    --else
    --    self.typeCtrl.selectedIndex = 0;
    --    self.selectCardIndex = 1;
    --end
    --self:showBtnName();
    if self.hasGetBigReward then
        UISysTips.AddMsg(getLanguage("ActivityTips2500114"));
        return;
    end
    local cfg = _M.cardItems[1];
    local rewardBigNumber = _M.serverData.param.rewardBigNumber;
    local selectReward = {};
    local rewardBig = decodeJsonStrToTabel(cfg.f_RewardBig);
    if table.nums(rewardBigNumber) < 1 then
        selectReward = decodeJsonStrToTabel(cfg.f_Reward);
    else
        for rewardk,rewardv in pairs(rewardBigNumber) do
            table.insert(selectReward, rewardBig[rewardv + 1])
        end
    end
    showUI(PanelNames.UIActivityCardTips, function(selectIndexs)
        if self.isClick then
            return;
        end
        self.isClick = true;
        local indexs = {};
        for k,v in pairs(selectIndexs) do
            indexs[k] = tonumber(v) - 1;
        end
        ActivityHandler.sendReqAppointRewardBigMessage(self.activeId, indexs);
    end, selectReward, 0, rewardBig);
end

function _M:ClickBtnAddLv()
    if self.selectCardIndex == 1 then
        -- 抽卡
        local cardItemsNum = #self.cardItems;
        if self.getRewardNum >= cardItemsNum then
            self:showResetRewardTips();
            return;
        end
        if self.serverData.param.freeCount == 1 then
            self:sendReqLotteryDraw();
            return;
        end
        local costId = self.tableData.f_Consume[1];
        local haveNum = logicMgr.ItemManager.getBagNumByTableID(costId);
        if haveNum < self.needCostNum then
            UISysTips.AddMsg(string.format(getLanguage("Character_Tips19"),self.costItemName));
            return;
        end
        if haveNum >= cardItemsNum then
            local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop)
            pop.Init(132, function (flag, isClose)
                if isClose == nil or not isClose then
                    if flag then
                        ActivityHandler.sendReqLotteryDrawMessage(self.activeId, 0);
                    else
                        self:sendReqLotteryDraw();
                    end
                end
            end, cardItemsNum - self.getRewardNum, self.costItemName);
        else
            self:sendReqLotteryDraw();
        end
    else
        -- 切换大奖
        if self.hasGetBigReward then
            UISysTips.AddMsg(getLanguage("ActivityTips2500108"));
            return;
        end
        local cfg = _M.cardItems[1];
        local rewardBigNumber = _M.serverData.param.rewardBigNumber;
        local selectReward = {};
        local rewardBig = decodeJsonStrToTabel(cfg.f_RewardBig);
        if table.nums(rewardBigNumber) < 1 then
            selectReward = decodeJsonStrToTabel(cfg.f_Reward);
        else
            for rewardk,rewardv in pairs(rewardBigNumber) do
                table.insert(selectReward, rewardBig[rewardv + 1])
            end
        end
        showUI(PanelNames.UIActivityCardTips, function(selectIndexs)
            if self.isClick then
                return;
            end
            self.isClick = true;
            local indexs = {};
            for k,v in pairs(selectIndexs) do
                indexs[k] = tonumber(v) - 1;
            end
            ActivityHandler.sendReqAppointRewardBigMessage(self.activeId, indexs);
        end, selectReward, 0, rewardBig);
    end
end

function _M:sendReqLotteryDraw()
    if self.selectItemIndex == 0 then
        UISysTips.AddMsg(getLanguage("ActivityTips2500107"));
        return;
    end
    if self.isClick then
        return;
    end
    self.isClick = true;
    ActivityHandler.sendReqLotteryDrawMessage(self.activeId, self.selectItemIndex);
end

function _M:ClickBtnSign1()
    if self.selectTabIndex == 1 then
        return;
    end
    self.typeCtrl.selectedIndex = 0;
    self.selectTabIndex = 1;
    self.selectCardIndex = 1;
    self.BtnSign1.selected = true;
    self.BtnSign2.selected = false;
    self:showBtnName();
end

function _M:ClickBtnSign2()
    if self.selectTabIndex == 2 then
        return;
    end
    self.typeCtrl.selectedIndex = 1;
    self.selectTabIndex = 2;
    self.BtnSign1.selected = false;
    self.BtnSign2.selected = true;
    self:refreshTask();
end

function _M:ClickBtnReset()
    if self.getRewardNum < 1 then
        UISysTips.AddMsg(getLanguage("ActivityTips2500113"));
        return;
    end
    if self.getRewardNum >= #self.cardItems then
        self:showResetRewardTips();
        return;
    end
    local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop)
    pop.Init(130, function (flag)
        if flag then
            ActivityHandler.sendReqResetStarsCardMessage(self.activeId);
        end
    end)
end

-- 显示
function _M:OnEnable()
    logicMgr.HeroManager.setGetNewHeroShow(true);
    self.playEffect = true;
    self:Refresh();
end

function _M:Refresh()
    if self.activeId == nil or not _M.hasInit then
        self:OnBack();
        return;
    else
        self.tableData, self.serverData = logicMgr.ActiveManager.getActiveData(self.activeId);
        if self.serverData == nil then
            self:OnBack();
            return;
        end
    end
    self.isClick = false;
    self:initView();
    self:refreshTask();
    self:updateResource();
    self:showBtnName();
    self.redCtrl.selectedIndex = self.serverData.taskRed;
end

function _M:showSelectdCard()
    if self.selectTabIndex ~= 1 or self.cardItems == nil or self.cardObjItems == nil then
        return;
    end
    for i = 1, #self.cardItems do
        local cardId = self.serverData.cardIndexs[tostring(i)];
        if cardId == nil then
            local itemObj = self.cardObjItems[tostring(i)];
            if itemObj ~= nil then
                if self.selectItem ~= nil then
                    self.selectItem.selected = false;
                end
                self.selectItem = itemObj;
                self.selectItem.selected = true;
                self.selectItemIndex = i;
            end
            break;
        end
    end
end

function _M:showBtnName()
    self.needItem.visible = false;
    if self.selectCardIndex == 1 then
        --抽奖
        if self.serverData.param.freeCount == 1 then
            self.btnAddLv.text = getLanguage("ActivityTips2500105");
        else
            self.btnAddLv.text = getLanguage("ActivityTips2500111");
            self.needItem.visible = true;
        end
        self.shopBtn.text = getLanguage("ActivityTips2500112");
    else
        --切换大奖
        self.btnAddLv.text = getLanguage("ActivityTips2500112");
        self.shopBtn.text = getLanguage("ActivityTips2500111");
    end
end

function _M:updateResource()
    local haveNum = logicMgr.ItemManager.getBagNumByTableID(self.tableData.f_ResourceShow[1]);
    self.CoinLb.title = getNumString(haveNum);
    haveNum = logicMgr.ItemManager.getBagNumByTableID(self.tableData.f_ResourceShow[2]);
    self.CashLb.title = getNumString(haveNum);
    haveNum = logicMgr.ItemManager.getBagNumByTableID(self.tableData.f_Consume[1]);
    if haveNum >= self.needCostNum then
        self.costNum.text = "[color=#D3D9DD]" .. self.needCostNum .. "[/color]";
    else
        self.costNum.text = "[color=#FF2A4D]" .. self.needCostNum .. "[/color]";
    end
end

function _M:initView()
    if self.cardItems ~= nil then
        return;
    end
    self.CoinLb.icon = EnumConst.AtlasPath.ItemAtlas .. dataTable.getDataTableByStringId("t_item", self.tableData.f_ResourceShow[1]).f_Icon;
    self.CashLb.icon = EnumConst.AtlasPath.ItemAtlas .. dataTable.getDataTableByStringId("t_item", self.tableData.f_ResourceShow[2]).f_Icon;
    self.costIcon.url = EnumConst.AtlasPath.ItemAtlas .. dataTable.getDataTableByStringId("t_item", self.tableData.f_Consume[1]).f_Icon;
    self.needCostNum = tonumber(self.tableData.f_Consume[2]);
    self.title.text = getLanguage(self.tableData.f_ActivityName);
    self.costItemName = logicMgr.ItemManager.getItemConstDatabyModelId(self.tableData.f_ResourceShow[2]).name;

    self.cardItems = {};
    self.cardObjItems = {};
    self.cardRewardObjItems = {};
    self.tableRewardData = dataTable.getDataTable(self.tableData.f_ItemTableName);
    for k,v in pairs(self.tableRewardData) do
        if tonumber(v.f_ActivityId) == tonumber(self.activeId) then
            table.insert(self.cardItems, v);
        end
    end
    table.sort(self.cardItems, function(a, b)
        return tonumber(a.f_CardsType) > tonumber(b.f_CardsType);
    end)
    local itemNum = #self.cardItems;
    self.getRewardNum = 0;
    self.list.numItems = itemNum;
    --self.cardList.numItems = itemNum;
    self:showResetRewardTips();
    self:showSelectdCard();
    self:refreshRewardInfo();
end

function _M.lotteryDraw(accStarCards, itemList)
    if _M.selectItem ~= nil then
        _M.selectItem.selected = false;
        _M.selectItem = nil;
    end
    _M.selectItemIndex = 0;
    --if table.nums(accStarCards) == 1 then
    --    -- 1抽
    --    local objIndex;
    --    local rewardId;
    --    for k, v in pairs(accStarCards) do
    --        objIndex = v.index;
    --        rewardId = v.id;
    --    end
    --    -- 播特效
    --    _M.bgClose.onClick:Clear();
    --    _M.cardShowCtrl.selectedIndex = 1;
    --    _M.getCardIcon.url = _M.tableData.f_CardsBG;
    --    _M.cardTrans:Play();
    --    _M:removeTimer();
    --    _M.cardEffectTimer = utimer.timeCall(function()
    --        _M:removeTimer();
    --        Util.PlayAction(_M.spineFore.wrapTarget, "idle2", false);
    --
    --        _M.cardEffectTimer = utimer.timeCall(function()
    --            local cfg = _M.tableRewardData[tostring(rewardId)];
    --            _M:playBackEffect(cfg.f_CardsType);
    --            local itemObj = _M.cardObjItems[tostring(objIndex)];
    --            itemObj.onClick:Clear();
    --            local bg = itemObj:GetChild("BG");
    --            bg.url = cfg.f_Cards;
    --            _M.getCardIcon.url = cfg.f_Cards;
    --
    --            _M.bgClose.onClick:Add(function()
    --                _M.cardShowCtrl.selectedIndex = 0;
    --                _M:getFetchReward(itemList);
    --            end);
    --
    --        end,150,1);
    --
    --    end,500,1);
    --else
    --    -- 多抽
    --    for k, v in pairs(accStarCards) do
    --        local itemObj = _M.cardObjItems[tostring(v.index)];
    --        -- 播特效
    --        itemObj.onClick:Clear();
    --        local cfg = _M.tableRewardData[tostring(v.id)];
    --        local bg = itemObj:GetChild("BG");
    --        bg.url = cfg.f_Cards;
    --    end
    --    _M:getFetchReward(itemList);
    --end

    local rewardCfg;
    local rewardType = -1;
    for k, v in pairs(accStarCards) do
        local cfg = _M.tableRewardData[tostring(v.id)];
        if cfg.f_RewardPool == 3 then
            _M.hasGetBigReward = true;
        end
        _M.getRewardNum = _M.getRewardNum + 1;
        if cfg.f_CardsType > rewardType then
            rewardType = cfg.f_CardsType;
            rewardCfg = cfg;
        end
    end
    -- 播特效
    _M.bgClose.onClick:Clear();
    _M.cardShowCtrl.selectedIndex = 1;
    local colorIndex = 3 - rewardCfg.f_CardsType;
    _M.colorTypeCtrl.selectedIndex = colorIndex;
    _M.colorTypeCtrl1.selectedIndex = colorIndex;
    _M.colorTypeCtrl2.selectedIndex = colorIndex;
    _M.getCardIcon.url = _M.tableData.f_CardsBG;
    _M.cardTrans:Play();
    _M:removeTimer();
    _M.cardEffectTimer = utimer.timeCall(function()
        local color = effectColor1;
        if type == 3 then
            color = effectColor4;
        elseif type == 2 then
            color = effectColor3;
        elseif type == 1 then
            color = effectColor2;
        elseif type == 0 then
            color = effectColor1;
        end

        _M:removeTimer();
        _M.glowCtrl.selectedIndex = 1;
        _M.effectSanguang:Play();
        _M:addBeForeEffect(color)

        _M.cardEffectTimer = utimer.timeCall(function()
            _M:playBackEffect(rewardCfg.f_CardsType, itemList);

            for k, v in pairs(accStarCards) do
                local itemObj = _M.cardObjItems[tostring(v.index)];
                itemObj.onClick:Clear();
                local cfg = _M.tableRewardData[tostring(v.id)];
                local bg = itemObj:GetChild("BG");
                bg.url = cfg.f_Cards;
                local spine = itemObj:GetController("Spine");
                spine.selectedIndex = 1;
            end
            _M.getCardIcon.url = rewardCfg.f_Cards;

        end, 150, 1);

    end, 500, 1);

    --for k, v in pairs(accStarCards) do
    --    local cfg = _M.tableRewardData[tostring(v.id)];
    --    if cfg.f_RewardPool == 3 then
    --        _M.hasGetBigReward = true;
    --    end
    --    _M.getRewardNum = _M.getRewardNum + 1;
    --    local btnList = _M.cardRewardObjItems[tostring(v.id)];
    --    if cfg.f_RewardPool == 3 then
    --        local rewardBigNumber = _M.serverData.param.rewardBigNumber;
    --        if table.nums(rewardBigNumber) < 1 then
    --            _M:updateReward(btnList, cfg.f_Reward, _M:isGetReward(v.id));
    --        else
    --            local rewardBig = decodeJsonStrToTabel(cfg.f_RewardBig);
    --            local selectReward = {};
    --            for rewardk,rewardv in pairs(rewardBigNumber) do
    --                table.insert(selectReward, rewardBig[rewardv + 1])
    --            end
    --            _M:updateReward(btnList, selectReward, _M:isGetReward(v.id));
    --        end
    --    else
    --        _M:updateReward(btnList, cfg.f_Reward, _M:isGetReward(v.id));
    --    end
    --end
    _M:showSelectdCard();
end

function _M:playBackEffect(type, itemList)
    local color = effectColor1;
    if type == 3 then
        color = effectColor4;
    elseif type == 2 then
        color = effectColor3;
    elseif type == 1 then
        color = effectColor2;
    elseif type == 0 then
        color = effectColor1;
    end
    self:removeTimer();
    self.cardEffectTimer = utimer.timeCall(function()
        self:removeTimer();
        self:addEffect(color);
        _M.loopCtrl.selectedIndex = 1;
        --_M.effectXunhuan:Play(-1, 0, nil);
    end,100,1);
    _M.bgClose.onClick:Add(function()
        self.effectNode.visible = false;
        self.effectNode1.visible = false;
        _M.loopCtrl.selectedIndex = 0;
        self.cardShowCtrl.selectedIndex = 0;
        _M.glowCtrl.selectedIndex = 0;
        _M:getFetchReward(itemList);
    end);
end

function _M:addBeForeEffect(color)
    if self.shuiPaoObj1 == nil then
        resMgr:LoadModelPrefab("UIEff/UI_chouka_01", function(go)
            if self.shuiPaoDrawWrapper1 == nil then
                self.shuiPaoDrawWrapper1 = GoWrapper.New();
            end
            if (self.shuiPaoObj1~= nil) then
                destroyImmediate(self.shuiPaoObj1);
            end
            self.shuiPaoObj1 = newObject(go);
            self.shuiPaoObj1:AddComponent(TCanvas);
            self.shuiPaoObj1:AddComponent(TChangeSortingOrder);
            self.shuiPaoDrawWrapper1:SetWrapTarget(self.shuiPaoObj1, false);
            Util.Lua_SetLocalPositionXYZ(self.shuiPaoObj1.transform, 0, 0, 0);
            self.effectNode1:SetNativeObject(self.shuiPaoDrawWrapper1);
            self.beEffectColor1 = {};
            --for i = 1, 3 do
                table.insert(self.beEffectColor1, self.shuiPaoObj1.transform:Find("dot_01"):GetComponent("ParticleSystem"));
            --end
            for k, v in pairs(self.beEffectColor1) do
                Util.Lua_SetParticleMainStartColor(v,color.r,color.g,color.b,color.a);
            end
        end)
    else
        if self.beEffectColor1 ~= nil then
            for k, v in pairs(self.beEffectColor1) do
                Util.Lua_SetParticleMainStartColor(v,color.r,color.g,color.b,color.a);
            end
        end
    end
    self.effectNode1.visible = true;
end

function _M:addEffect(color)
    if self.shuiPaoObj == nil then
        resMgr:LoadModelPrefab("UIEff/UI_chouka_02", function(go)
            if self.shuiPaoDrawWrapper == nil then
                self.shuiPaoDrawWrapper = GoWrapper.New();
            end
            if (self.shuiPaoObj~= nil) then
                destroyImmediate(self.shuiPaoObj);
            end
            self.shuiPaoObj = newObject(go);
            self.shuiPaoObj:AddComponent(TCanvas);
            self.shuiPaoObj:AddComponent(TChangeSortingOrder);
            self.shuiPaoDrawWrapper:SetWrapTarget(self.shuiPaoObj, false);
            Util.Lua_SetLocalPositionXYZ(self.shuiPaoObj.transform, 0, 0, 0);
            self.effectNode:SetNativeObject(self.shuiPaoDrawWrapper);
            self.effectColor = {};
            for i = 1, 3 do
                table.insert(self.effectColor, self.shuiPaoObj.transform:Find("dot_0"..i):GetComponent("ParticleSystem"));
            end
            for k, v in pairs(self.effectColor) do
                Util.Lua_SetParticleMainStartColor(v,color.r,color.g,color.b,color.a);
            end
        end)
    else
        if self.effectColor ~= nil then
            for k, v in pairs(self.effectColor) do
                Util.Lua_SetParticleMainStartColor(v,color.r,color.g,color.b,color.a);
            end
        end
    end
    self.effectNode.visible = true;
end

function _M:getFetchReward(data)
    logicMgr.HeroManager.showNewHero(function ()
        if data ~= nil then
            local rewards = {};
            for k,v in pairs(data) do
                local itemData = logicMgr.CostManager.getItemOrEquipDataByIdAndNum(v.itemModelId, v.num);
                if itemData ~= nil then
                    table.insert(rewards, itemData);
                end
            end
            if #rewards > 0 then
                ShowSpItemGetPanel(rewards, function()
                    self:showResetRewardTips();
                end, false)
            else
                self:showResetRewardTips();
            end
        end
    end);
end

function _M:showResetRewardTips()
    if self.getRewardNum >= #self.cardItems then
        local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop)
        pop.Init(131, function (flag)
            if flag then
                ActivityHandler.sendReqResetStarsCardMessage(self.activeId);
            end
        end)
    end
end

function _M.appointRewardBig()
    --local cfg = _M.cardItems[1];
    --local itemId = cfg.f_id;
    --local btnList = _M.cardRewardObjItems[tostring(cfg.f_id)];
    --local rewardBigNumber = _M.serverData.param.rewardBigNumber;
    --if table.nums(rewardBigNumber) < 1 then
    --    _M:updateReward(btnList, cfg.f_Reward, _M:isGetReward(itemId));
    --else
    --    local rewardBig = decodeJsonStrToTabel(cfg.f_RewardBig);
    --    local selectReward = {};
    --    for rewardk,rewardv in pairs(rewardBigNumber) do
    --        table.insert(selectReward, rewardBig[rewardv + 1])
    --    end
    --    _M:updateReward(btnList, selectReward, _M:isGetReward(itemId));
    --end
    _M:refreshRewardInfo();
end

function _M.resetStarsCard()
    _M.cardObjItems = {};
    _M.cardRewardObjItems = {};
    _M.getRewardNum = 0;
    local itemNum = #_M.cardItems;
    _M.list.numItems = itemNum;
    --_M.cardList.numItems = itemNum;
    _M.hasGetBigReward = false;
    _M.selectItemIndex = 0;
    _M:showSelectdCard();
end

function _M.refreshCardItem(index, itemObj)
    index = index + 1;
    local bg = itemObj:GetChild("BG");
    local spine = itemObj:GetController("Spine");
    itemObj.onClick:Clear();
    local cardId = _M.serverData.cardIndexs[tostring(index)];
    if cardId == nil then
        bg.url = _M.tableData.f_CardsBG;
        spine.selectedIndex = 0;
        itemObj.onClick:Add(function()
            if _M.selectItem ~= nil then
                _M.selectItem.selected = false;
            end
            _M.selectItem = itemObj;
            _M.selectItem.selected = true;
            _M.selectItemIndex = index;
        end);
    else
        spine.selectedIndex = 1;
        local cfg = _M.tableRewardData[tostring(cardId)];
        if cfg.f_RewardPool == 3 then
            _M.hasGetBigReward = true;
        end
        bg.url = cfg.f_Cards;
        _M.getRewardNum = _M.getRewardNum + 1;
    end
    _M.cardObjItems[tostring(index)] = itemObj;
end

function _M:refreshRewardInfo()
    local cfg = _M.cardItems[1];
    local rewardBigNumber = _M.serverData.param.rewardBigNumber;
    local rewardId = 0;
    if table.nums(rewardBigNumber) < 1 then
        local rewardBig = decodeJsonStrToTabel(cfg.f_Reward);
        rewardId = rewardBig[1][1];
    else
        local rewardBig = decodeJsonStrToTabel(cfg.f_RewardBig);
        for rewardk,rewardv in pairs(rewardBigNumber) do
            rewardId = rewardBig[rewardv + 1][1];
            break;
        end
    end
    self.shopBtn.icon = EnumConst.AtlasPath.ItemAtlas .. dataTable.getDataTableByStringId("t_item", rewardId).f_Icon;
end

function _M.refreshCardRewardItem(index, itemObj)
    index = index + 1;
    local bg = itemObj:GetChild("BG");
    local btnList = itemObj:GetChild("BtnList");
    local spineCtrl = itemObj:GetController("spine");
    spineCtrl.selectedIndex = 0;
    if index == 1 then
        spineCtrl.selectedIndex = 1;
    end
    local cfg = _M.cardItems[index];
    bg.url = cfg.f_Cards;
    local itemId = cfg.f_id;
    _M.cardRewardObjItems[tostring(cfg.f_id)] = btnList;
    if cfg.f_RewardPool == 3 then
        local rewardBigNumber = _M.serverData.param.rewardBigNumber;
        if table.nums(rewardBigNumber) < 1 then
            _M:updateReward(btnList, cfg.f_Reward, _M:isGetReward(itemId));
        else
            local rewardBig = decodeJsonStrToTabel(cfg.f_RewardBig);
            local selectReward = {};
            for rewardk,rewardv in pairs(rewardBigNumber) do
                table.insert(selectReward, rewardBig[rewardv + 1])
            end
            _M:updateReward(btnList, selectReward, _M:isGetReward(itemId));
        end
    else
        _M:updateReward(btnList, cfg.f_Reward, _M:isGetReward(itemId));
    end
end

function _M:isGetReward(itemId)
    local accStarCards = _M.serverData.cardIndexs;
    for k,v in pairs(accStarCards) do
        if tonumber(v) == tonumber(itemId) then
            return true;
        end
    end
    return false;
end

function _M:updateReward(item,reward,isGet)
    item:RemoveChildrenToPool()
    if reward == nil then
        return;
    end
    for i = 1, #reward do
        local obj = item:AddItemFromPool();
        logicMgr.CostManager.RefreshItem(obj, reward[i]);
        local dialogNode = obj:GetChild("dialog");
        local dialog = obj:GetController("dialog");
        local mask = obj:GetController("mask");
        if isGet then
            dialogNode.visible = true;
            dialog.selectedIndex = 1;
            mask.selectedIndex = 1;
        else
            dialogNode.visible = false;
            dialog.selectedIndex = 0;
            mask.selectedIndex = 0;
        end
    end
end

function _M:refreshTask()
    if self.selectTabIndex ~= 2 then
        return;
    end
    self.taskItems = self.serverData.taskItems;
    local stateList = {}
    for k,v in pairs(self.taskItems) do
        stateList[v.f_ActTaskId] = self:GetRewardState(v)
    end
    table.sort(self.taskItems,function(a,b)
        local aState = stateList[a.f_ActTaskId]
        local bState = stateList[b.f_ActTaskId]
        local aw = aState == 1 and 10 or 1
        aw = aState >= 11 and aState or aw
        local bw = bState == 1 and 10 or 1
        bw = bState >= 11 and bState or bw
        if aw ~= bw then
            return aw > bw
        end
        aw = aState == 0 and 10 or 1
        aw = aState >= 11 and aState or aw
        bw = bState == 0 and 10 or 1
        bw = bState >= 11 and bState or bw
        if aw ~= bw then
            return aw > bw
        end
        return a.f_ActTaskId < b.f_ActTaskId
    end)

    self.taskList.numItems = #self.taskItems;
    self.taskList:ScrollToView(0);
    if self.playEffect then
        self.playEffect = false;
        HelperFunc.ListTranstionPlay(self.taskList);
    end
end

--0:未完成，1：完成未领，2：完成已领
function _M:GetRewardState(itemCfg)
    local data = self.serverData.param.recvRewardCount[tostring(itemCfg.f_ActTaskId)];
    if itemCfg.f_TurntableTask == SPECIAL_RECHARGE_TYPE then
        --每充值30元可获得
        local totalPay = self.serverData.param.totalPay or 0;
        local recvCount = self.serverData.param.recvCount or 0;
        local count = math.floor(totalPay / itemCfg.f_Progress)
        local bigNum = tonumber(itemCfg.f_BigNum);
        if bigNum > 0 and recvCount >= bigNum then
            return 2;
        end
        if count > recvCount and (bigNum < 1 or recvCount < bigNum) then
            return 12;
        end
        return 11;
    end
    if data and data.recvCount == 1 then
        return 2;
    end
    if (data and data.process >= itemCfg.f_Progress) then
        return 1;
    end
    return 0;
end

function _M.refreshTaskItem(index, itemObj)
    index = index + 1
    local statusCtrl = itemObj:GetController("status");
    local title = itemObj:GetChild("title")
    local tips = itemObj:GetChild("Tips")
    local progress = itemObj:GetChild("n10");
    local btnGo = itemObj:GetChild("btn_Go");
    local btnGet = itemObj:GetChild("btn_Get");
    local effect = itemObj:GetChild("effect_tzsl")
    local cfg = _M.serverData.taskItems[index];
    logicMgr.CostManager.RefreshItemList(itemObj:GetChild("itemList"), cfg.f_Award);
    local count = 0
    if cfg.f_TurntableTask == SPECIAL_RECHARGE_TYPE then
        local totalPay = _M.serverData.param.totalPay or 0;
        local recvCount = _M.serverData.param.recvCount or 0;
        local bigNum = tonumber(cfg.f_BigNum);
        count = math.floor(totalPay / cfg.f_Progress)
        count = math.max(count - recvCount, 0)
        if bigNum > 0 then
            count = math.min(bigNum - recvCount, count)
        end
        tips.text = "";
        progress.visible = false;
        title.text = string.format(getLanguage(cfg.f_ActTaskDesc), count, recvCount, bigNum);
    else
        local data = _M.serverData.param.recvRewardCount[tostring(cfg.f_ActTaskId)];
        local process = 0
        if data ~= nil then
            count = data.process;
            process = count;
        end
        title.text = string.format(getLanguage(cfg.f_ActTaskDesc), count);
        tips.text = string.format(getLanguage("ActivityDesc1100304"),math.min(process,cfg.f_Progress),cfg.f_Progress)
        progress.value = process/cfg.f_Progress*100
    end

    local state = _M:GetRewardState(cfg)
    if state == 11 then
        state = 0;
    elseif state == 12 then
        state = 1;
    end
    statusCtrl.selectedIndex = state
    btnGo.onClick:Clear();
    btnGet.onClick:Clear();
    if state == 0 then --未完成
        btnGo.onClick:Add(function()
            if _M.isClick then
                return;
            end
            _M.isClick = true
            _M:OnBack();
            closeUI(PanelNames.UIActivity)
            showUI(PanelNames.UIHeadMenu);
            if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Cost, true) then
                logicMgr.HeroManager.setGetNewHeroShow();
                logicMgr.CostManager.gotoCostById();
            end
        end)
    elseif state == 1 then--未领取
        btnGet.onClick:Add(function()
            if _M.isClick then
                return;
            end
            _M.isClick = true
            if cfg.f_TurntableTask == SPECIAL_RECHARGE_TYPE then
                ActivityHandler.sendReqActivityDrawRewardMessage({_M.activeId, tonumber(cfg.f_ActTaskId)});
            else
                ActivityHandler.sendReqActivityDrawRewardMessage({_M.activeId, tonumber(cfg.f_ActTaskId)});
            end
        end)
    end
end

-- 隐藏
function _M:OnDisable()

end

function _M:removeTimer()
    if self.cardEffectTimer ~= nil then
        utimer.remove(self.cardEffectTime);
        self.cardEffectTimer = nil;
    end
end

-- 卸载
function _M:OnDestroy()
    logicMgr.HeroManager.setGetNewHeroShow();
    self:removeEvent();
    self:removeTimer();
    if self.shuiPaoObj then
        destroyImmediate(self.shuiPaoObj)
        self.shuiPaoObj = nil
    end
    if self.shuiPaoDrawWrapper then
        self.shuiPaoDrawWrapper:Dispose();
        self.shuiPaoDrawWrapper = nil;
    end
    if self.shuiPaoObj1 then
        destroyImmediate(self.shuiPaoObj1)
        self.shuiPaoObj1 = nil
    end
    if self.shuiPaoDrawWrapper1 then
        self.shuiPaoDrawWrapper1:Dispose();
        self.shuiPaoDrawWrapper1 = nil;
    end
    self.hasInit = false;
    self.playEffect = false;
    self.isClick = false;
    self.selectTabIndex = 1;
    self.selectCardIndex = 1;
    self.cardItems = nil;
    self.selectItem = nil;
    self.cardObjItems = nil;
    self.cardRewardObjItems = nil;
    self.selectItemIndex = 0;
    self.hasGetBigReward = false;
    self.getRewardNum = 0;
    self.effectColor = nil;
    self.beEffectColor1 = nil;
end

return _M;